cc.Class({
    extends: cc.Component,

    properties: {

        //地板[0,1,2] = [中, 左, 右]
        m_FloorBlock:[cc.Prefab],

        m_bar:[cc.Prefab],


        player:cc.Node,

        //类似金币这种可以吃（星星）
        m_star:cc.Prefab,
        //防护盾
        m_shield:cc.Prefab,
        
        //地刺
        m_hinder:cc.Prefab



        // view:cc.Node
    },

    // use this for initialization
    onLoad: function () {

    

    },


    start:function () {

        // this.m_BaseView = this.view.getComponent("BaseView");

        // this.changeFloor();


 
        
    },
    


    changeFloor:function()
    {

            var starPos = 0;
            var starY = cc.random0To1()*100 + 100; //控制地面高度
            for (var i = 0; i < this.m_BaseView.m_Floor.length; i++) {
                if( this.m_BaseView.m_Floor[i] == this.node && i != 0)
                {
                    starPos = this.m_BaseView.m_Floor[i-1].x + this.m_BaseView.m_Floor[i-1].width;
                }
                
            }

            this.node.y = starY;
            this.node.x = starPos;

            var left = cc.instantiate(this.m_FloorBlock[FloorBlockLeft]);
            this.node.addChild(left);
            left.x = 0;
            var posX = left.width;

            var count = this.myRandow(3,10);
            // count = 1;
            for (var i = 0; i < count; i++) {
                var floor = cc.instantiate(this.m_FloorBlock[FloorBlockMin]);
                this.node.addChild(floor);
                floor.x = posX;
                posX += floor.width;



            //随机添加护盾
            var b = cc.random0To1()
            if (b>=0.4) {
              //添加金币
                var star = cc.instantiate(this.m_star);
                this.node.addChild(star);
                star.x = floor.x;
                star.y = floor.y + 200;
            }

            if (b<0.3 & b>0.2) {
                var shield = cc.instantiate(this.m_shield);
                this.node.addChild(shield);
                shield.x = floor.x;
                shield.y = floor.y + 200;
            }

            if (b<0.2) {
                var hinder = cc.instantiate(this.m_hinder);
                this.node.addChild(hinder);
                hinder.x = floor.x;
                hinder.y = floor.y - 80;
            }

            }

            var right = cc.instantiate(this.m_FloorBlock[FloorBlockRight]);
            this.node.addChild(right);
            right.x = posX;

            posX += right.width;

            this.node.width = posX+this.myRandow(100,100);


            //概率添加空中障碍物

            var a = cc.random0To1()

            if (a>=0.5) {
            var bar = cc.instantiate(this.m_bar[0]);
            var barX = a*this.node.width;
            var barY = this.node.height -  bar.height/2;
            this.node.addChild(bar);
            bar.setPosition(barX,barY); 
            }
            //v = s/t
            //t = s/v
            var s = this.node.x+this.node.width;

            var move = cc.moveTo(s/200,cc.p(-(this.node.width),this.node.y));

            var sql = cc.sequence(move,cc.callFunc(function(){
                var floor = this.m_BaseView.m_Floor.shift();
                this.m_BaseView.m_Floor.push(floor);
                
                //console.error(this.node.children)
                for (let i = 0; i < this.node.children.length; i++) {
                    //console.log(this.node.children[i]);
                    this.node.children[i].destroy();
                }

                this.changeFloor();
            },this,this.node));
            this.node.runAction(sql);

    },

    setBaseView:function(view)
    {
        this.m_BaseView = view;
    },


    myRandow:function(min,max)
    {
        var count = cc.random0To1()*10000;
        count = count%((max-min)+min);
        return count;
    },



    // called every frame, uncomment this function to activate update callback
    update: function (dt) {
       
    },


});
